//
//  qieappChooseView.m
//  qieapp
//
//  Created by 吕金状 on 2017/11/13.
//  Copyright © 2017年 徐双. All rights reserved.
//

#import "qieappChooseView.h"


@interface qieappChooseView ()<UIPickerViewDelegate , UIPickerViewDataSource>
{
    NSString *destDateString;
}

@property (nonatomic , strong) UIButton *determineButton; /**<确定*/
@property (nonatomic , strong) UIButton *cancelButton; /**<取消*/
@property (nonatomic , strong) UIPickerView *datePicker;     /**<时间选择器*/
@property (nonatomic , strong) UIView *bgView; /**<*/
Copy void(^selectedHandle)(NSString *str);

@end

@implementation qieappChooseView

- (instancetype)initWithFrame:(CGRect)frame selectedHandle:(void (^)(NSString *))selectedHandle{
    if (self = [super initWithFrame:frame]) {
        self.alpha = 0;
        
        UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        _selectedHandle = selectedHandle;
        backButton.backgroundColor = [UIColor blackColor];
        backButton.alpha = .6;
        backButton.titleLabel.font = DEF_FontSize_13;
        [backButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
        _bgView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 200 / WIDTH_5S_SCALE, SCREEN_WIDTH, 200 / WIDTH_5S_SCALE)];
        _bgView.backgroundColor = getColor(whiteColor);
        [self addSubview:backButton];
        [self addSubview:_bgView];
        [_bgView addSubview:self.datePicker];
        [_bgView addSubview:self.determineButton];
        [_bgView addSubview:self.cancelButton];
        [self layoutFrame];
    }
    return self;
}

#pragma mark - init

- (UIButton *)determineButton{
    if (!_determineButton) {
        _determineButton = [[UIButton alloc]init];
        _determineButton.titleLabel.font = DEF_FontSize_13;
        [_determineButton setTitle:@"确定" forState:UIControlStateNormal];
        _determineButton.layer.masksToBounds = YES;
        _determineButton.layer.cornerRadius = 5;
        _determineButton.layer.borderColor = getColor(mainColor).CGColor;
        _determineButton.layer.borderWidth = 1;
        [_determineButton setTitleColor:getColor(mainColor) forState:UIControlStateNormal];
        [_determineButton addTarget:self action:@selector(determineButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _determineButton;
}

- (UIButton *)cancelButton{
    if (!_cancelButton) {
        _cancelButton = [[UIButton alloc]init];
        _cancelButton.titleLabel.font = DEF_FontSize_13;
        [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        _cancelButton.layer.masksToBounds = YES;
        _cancelButton.layer.cornerRadius = 5;
        _cancelButton.layer.borderColor = getColor(mainColor).CGColor;
        _cancelButton.layer.borderWidth = 1;
        [_cancelButton setTitleColor:getColor(mainColor) forState:UIControlStateNormal];
        [_cancelButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelButton;
}

/**
 *  时间选择器
 */
- (UIPickerView *)datePicker{
    if (!_datePicker) {
        _datePicker = [[UIPickerView alloc]init];
        // 显示选中框
        _datePicker.showsSelectionIndicator=YES;
        _datePicker.dataSource = self;
        _datePicker.delegate = self;
    }
    return _datePicker;
}

#pragma mark - Click

- (void)determineButtonClick{
    NSLog(@"确定");
    NSLog(@"返回选中的行%@",destDateString);
    if (_selectedHandle) {
        _selectedHandle(destDateString);
    }
    [self dismiss];
}

#pragma  mark - <UIPickerViewDelegate ,


// 返回选中的行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    
    destDateString = self.dataArray[row];
    
    _seletedIndex = row;
    
}

//返回当前行的内容,此处是将数组中数值添加到滚动的那个显示栏上
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [self.dataArray objectAtIndex:row];
    
}

#pragma  mark - <UIPickerViewDataSource>

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    return self.dataArray.count;
}

#pragma mark - frame

- (void)layoutFrame{
    __weak __typeof(self)weakSelf = self;
    
    [self.datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.bgView).with.offset(0);
        make.left.equalTo(weakSelf.bgView).with.offset(0  / WIDTH_5S_SCALE);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.height.mas_equalTo(200 / WIDTH_5S_SCALE);
    }];
    
    [self.determineButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.bgView).with.offset(10 / WIDTH_5S_SCALE);
        make.right.equalTo(weakSelf.bgView).with.offset( -10 / WIDTH_5S_SCALE);
        make.width.mas_equalTo(50 / WIDTH_5S_SCALE);
        make.height.mas_equalTo(25 / WIDTH_5S_SCALE);
    }];
    
    [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.bgView).with.offset(10 / WIDTH_5S_SCALE);
        make.left.equalTo(weakSelf.bgView).with.offset(10 / WIDTH_5S_SCALE);
        make.width.mas_equalTo(50 / WIDTH_5S_SCALE);
        make.height.mas_equalTo(25 / WIDTH_5S_SCALE);
    }];
}

//出现
-(void)show{
    [[UIApplication sharedApplication].delegate.window addSubview:self];
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 1;
    }];
}

//消失
-(void)dismiss{
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 0 ;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    destDateString = dataArray[0];
    [self.datePicker reloadAllComponents];
}

@end
